package main;

import main.dao.Order;
import main.common.strategy.DiscountStrategy;
import main.common.factory.impl.FruitFactory;
import main.common.strategy.impl.NoDiscountStrategy;
import main.common.strategy.impl.TenYuanOffForHundredDiscountStrategy;
import main.common.product.Fruit;
import main.common.product.impl.Apple;
import main.common.product.impl.Strawberry;

import java.util.HashMap;
import java.util.Map;

public class Main {

    public static FruitFactory fruitFactory = new FruitFactory();

    public static void main(String[] args) {

        Apple apple = fruitFactory.createApple(8, 0);
        Strawberry strawberry = fruitFactory.createStrawberry(13, 0);
        Fruit mango = fruitFactory.createMango(20, 0);
        Map<String, Fruit> fruitMap = new HashMap<>();
        fruitMap.put("apple", apple);
        fruitMap.put("strawberry", strawberry);
        fruitMap.put("mango", mango);

        // 计算总价
        customerA(fruitMap);
        customerB(fruitMap);

        //草莓限时打 8 折
        strawberry = fruitFactory.createStrawberry(13, 0.2);
        fruitMap.put("strawberry", strawberry);
        customerC(fruitMap);
        customerD(fruitMap);


    }


    private static void customerA(Map<String, Fruit> fruitMap) {
        //有一家超市，出售苹果和草莓。其中苹果 8 元/斤，草莓 13 元/斤。
        //现在顾客 A 在超市购买了若干斤苹果和草莓，需要计算一共多少钱？


        //购买了 2 斤苹果和 3 斤草莓
        HashMap<Fruit, Integer> orderMap = new HashMap<>();
        orderMap.put(fruitMap.get("apple"), 2);
        orderMap.put(fruitMap.get("strawberry"), 3);

        double total = calculate(orderMap, new Order(), NoDiscountStrategy.getInstance());
        System.out.println("顾客A购买总价: " + total);

    }

    private static void customerB(Map<String, Fruit> fruitMap) {
        //超市增加了一种水果芒果，其定价为 20 元/斤。
        //现在顾客 B 在超市购买了3斤苹果、 1斤草莓和2斤芒果，需计算一共需要多少钱？



        //购买了 3 斤苹果、 1 斤草莓和 2 斤芒果
        HashMap<Fruit, Integer> orderMap = new HashMap<>();
        orderMap.put(fruitMap.get("apple"), 3);
        orderMap.put(fruitMap.get("strawberry"), 1);
        orderMap.put(fruitMap.get("mango"), 2);

        double total = calculate(orderMap, new Order(), NoDiscountStrategy.getInstance());
        System.out.println("顾客B购买总价: " + total);

    }


    private static void customerC(Map<String, Fruit> fruitMap) {
        //超市做促销活动，草莓限时打 8 折。
        //现在顾客 C 在超市购买了若3斤苹果、 1斤草莓和2斤芒果，需计算一共需要多少钱？


        //购买了 3 斤苹果、 1 斤草莓和 4 斤芒果
        HashMap<Fruit, Integer> orderMap = new HashMap<>();
        orderMap.put(fruitMap.get("apple"), 3);
        orderMap.put(fruitMap.get("strawberry"), 1);
        orderMap.put(fruitMap.get("mango"), 4);

        double total = calculate(orderMap, new Order(), NoDiscountStrategy.getInstance());
        System.out.println("顾客C购买总价: " + total);


        ;
    }

    private static void customerD(Map<String, Fruit> fruitMap) {
        //促销活动效果明显，超市继续加大促销力度，购物满 100 减 10 块。
        //现在顾客 D 在超市购买了若干斤苹果、 草莓和芒果，需计算一共需要多少钱?


        //购买了 3 斤苹果、 1 斤草莓和 4 斤芒果
        HashMap<Fruit, Integer> orderMap = new HashMap<>();
        orderMap.put(fruitMap.get("apple"), 3);
        orderMap.put(fruitMap.get("strawberry"), 1);
        orderMap.put(fruitMap.get("mango"), 4);

        double total = calculate(orderMap, new Order(), TenYuanOffForHundredDiscountStrategy.getInstance());
        System.out.println("顾客D购买总价: " + total);

    }


    private static double calculate(HashMap<Fruit, Integer> fruitMap, Order order, DiscountStrategy discountStrategy) {

        //计算总价
        for(Fruit fruit : fruitMap.keySet()) {
            int quantity = fruitMap.get(fruit);
            order.addFruit(fruit, quantity);
        }
        return order.calculateTotal(discountStrategy);

    }
}